其他
Pandas技巧:万能转格式、轻松合并、压缩数据,让数据分析更高效
(给数据分析与开发加星标,提升数据技能)
转自:量子位-Roman Orac
Pandas实用技巧
import pandas as pd
import random
n = 10
df = pd.DataFrame(
{
"col1": np.random.random_sample(n),
"col2": np.random.random_sample(n),
"col3": [[random.randint(0, 10) for _ in range(random.randint(3, 5))] for _ in range(n)],
}
)
with open(‘analysis.html’, ‘w’) as f: f.write(df_html)
5个鲜为人知的Pandas技巧
date_from = “2019-01-01”
date_to = “2019-01-12”
date_range = pd.date_range(date_from, date_to, freq=”D”)
print(date_range)
import pandas as pd
df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=["a", "b", "c"])
report_name = example_report.xlsx
sheet_name = Sheet1
writer = pd.ExcelWriter(report_name, engine= xlsxwriter )
df.to_excel(writer, sheet_name=sheet_name, index=False)
workbook = writer.book
worksheet = writer.sheets[sheet_name]
# create a chart line object
chart = workbook.add_chart({ type : line })
# configure the series of the chart from the spreadsheet
# using a list of values instead of category/value formulas:
# [sheetname, first_row, first_col, last_row, last_col]
chart.add_series({
categories : [sheet_name, 1, 0, 3, 0],
values : [sheet_name, 1, 1, 3, 1],
})
# configure the chart axes
chart.set_x_axis({ name : Index , position_axis : on_tick })
chart.set_y_axis({ name : Value , major_gridlines : { visible : False}})
# place the chart on the worksheet
worksheet.insert_chart( E2 , chart)
# output the excel file
writer.save()
df.to_csv(‘random_data.csv’, index=False)
Python 数据处理库 pandas 进阶教程
Python 数据处理库 pandas 入门教程
看完本文有收获?请转发分享给更多人
关注「数据分析与开发」加星标,提升数据技能
好文章,我在看❤️